/* eslint-disable no-undef */
/**
 * ajax请求配置
 */
import axios from 'axios'
import config from '@/config/config'
import router from '@/router'
import store from '@/store'

const apiURL = config.api
// axios默认配置
axios.defaults.timeout = 60000// 超时时间
axios.defaults.baseURL = apiURL// 默认地址

// 整理数据
axios.defaults.transformRequest = function (data) {
  if (Object.prototype.toString.call(data) !== '[object FormData]') { // 对FormData对象不拦截
    data = JSON.stringify(data)
  }
  return data
}

// 路由请求拦截
// http request 拦截器
axios.interceptors.request.use(
  config => {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'
    config.headers['Authori-zation'] = `Bearer ${store.getters.token}`
    return config
  },
  error => {
    return Promise.reject(error.response)
  })

// 路由响应拦截
// http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.data.status === 400) {
      console.log('*********************')
      console.log(response)
      console.log('*********************')
      return response.data
    } else if (response.data.status >= 410000) {
      console.log(response.msg)
      if (store.getters.pl === 'H5') {
        router.push({ name: 'login', params: { timeout: true, redirect: store.getters.path } })
      } else if (store.getters.pl === 'ANDROID') {
        AndroidJsBridge.login()
      } else if (store.getters.pl === 'IOS') {
        window.iosCallHandler('pushLoginVC')
      }
      return false
    } else if (response.data.status === 200) {
      return response.data
    }
  },
  error => {
    return Promise.reject(error.response)// 返回接口返回的错误信息
  })
export default axios
